import Vue from 'vue';
import VueRouter from 'vue-router';
import VueResource from 'vue-resource';
import VueValidator from 'vue-validator';
import infiniteScroll from 'vue-infinite-scroll';
import configRouter from './config/router';
import global_ from './router/global';
//import vuePicturePreview from 'vue-picture-preview'

import App from './App';

import wechat from "./config/wechat";   //金蛋运动页面



Vue.config.debug = false;
Vue.use(VueRouter);
Vue.use(VueResource);
Vue.use(VueValidator);
Vue.use(infiniteScroll);
//Vue.use(vuePicturePreview)
//Vue.prototype.menu = "";

// HTTP相关
Vue.http.options.emulateJSON = true;
Vue.http.options.emulateHTTP = true;
Vue.http.options.crossOrigin = true;
Vue.http.options.withCredentials = true;

Vue.http.options.headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
};
//Vue.http.options.xhr = {withCredentials: true}





//全局验证
Vue.prototype.global = global_;
Vue.http.options.params = {
    'r_key': Vue.prototype.global.r_key,
    'p_key': Vue.prototype.global.p_key
};


/* Vue.http.interceptors.push(function(request, next) {

      // continue to next interceptor
      next(function(response) {
        // modify response
        //response.body = '...';
        console.log('aa');
      });
});
 */

const router = new VueRouter({
    hashhang: true,
    history: false, //html5模式 去掉锚点
    saveScrollPosition: false //记住页面的滚动位置 html5模式适用
});

router.beforeEach(function(transition) {
    /* if (transition.to.path == '/news/index' || transition.to.path == '/') {
       transition.redirect(transition.to.path+'?t=' +Math.random());
     } else {
         transition.next()
     }*/
    let {
        href,
        protocol,
        host,
        search,
        hash
    } = window.location
        //const pathname = '/frontend/' // 解决支付路径问题添加的前缀，替换成你的
    search = search || '?'
    hash = hash || '#/'
        //let newHref = `${protocol}//${host}${pathname}${search}${hash}`
    let newHref = `${protocol}//${host}${search}${hash}`
    if (newHref !== href) {
        window.location.replace(newHref)
            //console.log(newHref)
            //console.log(window.location.href)
    }
    transition.next()
})



router.afterEach(function(route) {
    console.log(route)
    var rout = route.to.name,
        wx_share = {
            title: '资讯会员',
            link: '',
            img: 'http://www.uximoney.com/themes/blue/img/logo-temp.png',
            desc: '',
            newsID: ''
        },
        shared = false;
    //console.log(route)
    switch (rout) {
        case "home":
            wx_share.title = "有喜财富—专注房产车辆抵押理财,资金银行存管,年化收益高达13.2%!"
            wx_share.desc = "安全理财不迷茫，有喜财富在护航!"
            shared = true;

            break;

        case "invest":
            if (route.to.query.productype == "group") {
                wx_share.title = "深圳房产直接抵押到您名下的理财"
                wx_share.desc = "年化收益高达12%，投资人直接放款给借款人，平台不接触资金!"
            } else {
                wx_share.title = "安全可靠的短期抵押理财，P2P投资就上有喜财富"
                wx_share.desc = "100元起投，资金银行存管，年化收益高达13.2%!"
            }
            shared = true;
            break;

        case "news":
            wx_share.title = "理财投资资讯，网贷相关知识，互联网金融新闻"
            wx_share.desc = "提供专业的理财小知识、资讯!"
            shared = true;
            break;

        case "group":
            wx_share.title = "抵.理财介绍，深圳房产直接抵押到投资人名下"
            wx_share.desc = "年化收益高达12%，投资人直接放款给借款人，平台不接触资金!"
            shared = true;
            break;

        case "invite":
            wx_share.title = "我在有喜财富理财很久啦，送你568元红包，撸起袖子加油赚!"
            wx_share.desc = "100元起投，资金银行存管，年化收益高达13.2%!"
            shared = true;
            break;
    }
    if (shared) {
        wx_share.link = window.location.href
        //console.log('before link:' + wx_share.link)
        //Vue.prototype.global.wxset(wx_share)
        wechat(wx_share)
    }

    setTimeout(function() {
        if (document.getElementById('_embed_v3_dc') != undefined) {
            console.log('ad')
            document.getElementById('_embed_v3_dc').setAttribute('style', 'display:none')
        }
    }, 200);

    /*var wx_share = {"title":'', "link":window.location.href, "img":'http://www.uximoney.com/themes/blue/img/logo-temp.png', "desc":''}
    wx_share.title = "有喜财富—专注房产车辆抵押理财,资金银行存管,年化收益高达13.2%!"
    wx_share.desc = "安全理财不迷茫，有喜财富在护航!"
    this.global.wxset(wx_share)*/

})

configRouter(router);
router.start(Vue.extend(App), 'app');
